package com.longrise.co.ui.mine.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.longrise.co.R;
import com.longrise.co.ui.carfiles.adapter.UploadPhotoAdapter;
import com.longrise.co.ui.carfiles.model.UploadPhotoBean;
import com.longrise.co.ui.mine.contract.MineContract;
import com.longrise.co.ui.mine.model.ComplaintDetailBean;
import com.longrise.co.ui.mine.model.MineModel;
import com.longrise.co.ui.mine.presenter.MinePresenter;
import com.longrise.co.utils.Constants;
import com.ly.common.base.BaseFragment;
import com.ly.common.commonutils.StringUtils;
import com.ly.common.commonutils.TimeUtil;
import com.ly.common.dialog.DialogOnClickListener;
import com.ly.common.dialog.MDAlertDialog;
import com.ly.common.imagePager.BigImagePagerActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by luoyang on 2017-7-21.
 */

public class ComplaintDetailFragment extends BaseFragment<MinePresenter, MineModel> implements MineContract.View<ComplaintDetailBean>, BaseQuickAdapter.OnRecyclerViewItemClickListener, DialogOnClickListener, View.OnClickListener {


    @Bind(R.id.head_title)
    TextView headTitle;
    @Bind(R.id.complaint_name)
    TextView complaintName;
    @Bind(R.id.ratingBar)
    RatingBar ratingBar;

    TextView complaintLocationTv;
    TextView complaintCallTv;
    TextView carNoValue;
    TextView detailDate;
    TextView detailDesc;

    RelativeLayout detailDescMore;
    TextView detailDescMoreValue;

    ImageView arrowRight;
    TextView complaintValue;
    RelativeLayout complaintValueRl;
    RelativeLayout uploadRl;

    @Bind(R.id.photos)
    RecyclerView photos;

    private View mHeaderView;

    private String mComplaintId;
    private String mAreaId;

    private boolean isEx;

    private UploadPhotoAdapter mAdapter;

    private List<String> mPhotoList = new ArrayList<>();
    private MDAlertDialog mCallDialog;
    private ComplaintDetailBean mData;

    @Override
    protected int getLayoutResource() {
        return R.layout.fg_complaint_detail;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    protected void initView() {
        mComplaintId = getArguments().getString("complaintId");
        mAreaId = getArguments().getString("areaId");
        setText(headTitle, "投诉详情");
        initHeadView();
        initRecyclerView();
        mPresenter.getComplaintDetail(mComplaintId, mAreaId);
    }

    private void initHeadView() {
        mHeaderView = LayoutInflater.from(getContext()).inflate(R.layout.fg_complaint_detail_header, null);
        if(mHeaderView == null) {
            return;
        }
        complaintLocationTv = (TextView) mHeaderView.findViewById(R.id.complaint_location_tv);
        complaintCallTv = (TextView) mHeaderView.findViewById(R.id.complaint_call_tv);
        carNoValue = (TextView) mHeaderView.findViewById(R.id.car_no_value);
        detailDate = (TextView) mHeaderView.findViewById(R.id.detail_date);
        detailDesc = (TextView) mHeaderView.findViewById(R.id.detail_desc);

        detailDescMore = (RelativeLayout) mHeaderView.findViewById(R.id.detail_desc_more);
        detailDescMoreValue = (TextView) mHeaderView.findViewById(R.id.detail_desc_more_value);

        arrowRight = (ImageView) mHeaderView.findViewById(R.id.arrow_right);

        complaintValueRl = (RelativeLayout) mHeaderView.findViewById(R.id.complaint_value_rl);
        complaintValue = (TextView) mHeaderView.findViewById(R.id.complaint_value);

        uploadRl = (RelativeLayout) mHeaderView.findViewById(R.id.upload_rl);

        ImageView call = (ImageView) mHeaderView.findViewById(R.id.complaint_call_iv);
        RelativeLayout complaintRl = (RelativeLayout) mHeaderView.findViewById(R.id.complaint_rl);

        call.setOnClickListener(this);
        complaintRl.setOnClickListener(this);

    }

    private void initRecyclerView() {

        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 3);
        photos.setLayoutManager(layoutManager);
        mAdapter = new UploadPhotoAdapter(getContext(), null);

        mAdapter.openLoadMore(false);
        mAdapter.setOnRecyclerViewItemClickListener(this);
        photos.setAdapter(mAdapter);
        mAdapter.addHeaderView(mHeaderView);
    }

    public static ComplaintDetailFragment newInstance(String complaintId, String areaId) {

        Bundle args = new Bundle();
        args.putString("complaintId", complaintId);
        args.putString("areaId", areaId);
        ComplaintDetailFragment fragment = new ComplaintDetailFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void requestSuccess(ComplaintDetailBean bean) {
        if(bean == null) {
            return;
        }
        try {

            mData = bean;
            setText(complaintName, bean.getCompany());

//        int star = new BigDecimal(bean.getStar()).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
            if (!StringUtils.isEmpty(bean.getStar())) {
                ratingBar.setRating(Float.parseFloat(bean.getStar()));
            }

            setText(complaintLocationTv, bean.getCompanyaddress());
            setText(complaintCallTv, bean.getBusphone());
            setText(carNoValue, bean.getVehicleplatenumber());
            setText(detailDate, TimeUtil.getDateYMD(bean.getRepairdate()));

            if(!StringUtils.isEmpty(bean.getFaultdescript()) && bean.getFaultdescript().length() > 20) {
                detailDesc.setVisibility(View.GONE);
                detailDescMore.setVisibility(View.VISIBLE);
                detailDescMoreValue.setText(bean.getFaultdescript());
            } else {
                detailDesc.setVisibility(View.VISIBLE);
                detailDescMore.setVisibility(View.GONE);
                setText(detailDesc, bean.getFaultdescript());
            }
            setText(complaintValue, bean.getEvaluatedetails());
            List<UploadPhotoBean> list = getPhotoListBean(bean.getComplaintimg());
            if(list != null) {
                mAdapter.setNewData(list);
            } else {
                uploadRl.setVisibility(View.GONE );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        isEx = true;
        setComplaintDetail();
    }

    private void initCallDialog() {
        mCallDialog = new MDAlertDialog.Builder(getContext())
                .setContentText("确定拨打该企业电话？")
                .setOnclickListener(this)
                .build();
    }

    private List<UploadPhotoBean> getPhotoListBean(String photos) {
        if(StringUtils.isEmpty(photos)) {
            return null;
        }
        String[] photoList = photos.split(",");
        if(photoList == null || photoList.length == 0) {
            return null;
        }

        List<UploadPhotoBean> list = new ArrayList<>();
        for(String photo : photoList) {
            if(!StringUtils.isEmpty(photo)) {
                list.add(new UploadPhotoBean(Constants.UPLOAD_TYPE_ONE, photo.trim()));
                mPhotoList.add(photo.trim());
            }
        }
        return list;

    }

    @Override
    public void onRequestComplete() {

    }

    private void setComplaintDetail() {
        complaintValueRl.setVisibility(isEx ? View.VISIBLE : View.GONE);
        arrowRight.setImageResource(isEx ? R.drawable.top : R.drawable.arrow_right);
    }

    @OnClick(R.id.head_back)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.head_back:
                _mActivity.onBackPressed();
                break;
            case R.id.complaint_rl:
                isEx = !isEx;
                setComplaintDetail();
                break;
            case R.id.confirm:
                break;
            case R.id.complaint_call_iv:
                if(mCallDialog == null) {
                    initCallDialog();
                }
                mCallDialog.show();
                break;
        }
    }

    @Override
    public void onItemClick(View view, int i) {
        BigImagePagerActivity.startImagePagerActivity(getActivity(), mPhotoList, 0);
    }

    @Override
    public void clickLeftButton(View view) {
        if(mCallDialog != null) {
            mCallDialog.dismiss();
        }
    }

    @Override
    public void clickRightButton(View view) {
        //拨打电话
        if(mData != null && !StringUtils.isEmpty(mData.getBusphone())) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:" + mData.getBusphone()));
            startActivity(intent);
        }

        if(mCallDialog != null) {
            mCallDialog.dismiss();
        }
    }

    @Override
    public void onClick(View v) {
        onViewClicked(v);
    }
}
